﻿using ColorWanted.Properties;
using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

namespace ColorWanted.clipboard
{
    public delegate void RemoveDelegete(ClipboardCtrl ctrl, string filename);
    public partial class ClipboardCtrl : UserControl
    {
        public string Date { get; set; }
        public RemoveDelegete OnRemove;
        private StarDialog starDialog;
        private bool starred = false;
        private bool onStarPage = false;
        private string remark = null;
        private TextBox tbRemark = null;

        public ClipboardCtrl()
        {
            InitializeComponent();
        }

        public ClipboardCtrl(string name)
        {
            InitializeComponent();

            // 如果路径中包含 star，表示是在收藏页展示
            var starFlag = Path.DirectorySeparatorChar + "star" + Path.DirectorySeparatorChar;
            if (name.Contains(starFlag))
            {
                onStarPage = true;
                name = name.Replace(starFlag, Path.DirectorySeparatorChar.ToString())
                    .Replace('#', Path.DirectorySeparatorChar);
            }

            var data = ClipboardManager.Read(name);
            FileName = name;
            Date = lbTime.Text = data.Date;
            tbContent.Text = data.Data;

            // 加载收藏信息
            var _remark = ClipboardManager.IsStarred(FileName);
            if (_remark != null)
            {
                remark = _remark;
                UpdateStarStatus(true);
            }
        }

        public string FileName { get; }

        private void UpdateStarStatus(bool status)
        {
            starred = status;

            if (tbRemark == null)
            {
                tbRemark = new TextBox()
                {
                    Width = 270,
                    Height = 50,
                    Top = 32,
                    Left = 13,
                    Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top,
                    AutoSize = false,
                    BackColor = Color.White,
                    BorderStyle = BorderStyle.None,
                    Multiline = true,
                    ReadOnly = true,
                    ForeColor = Color.Blue,
                    Cursor = Cursors.Default,
                };
                this.Controls.Add(tbRemark);
                toolTip1.SetToolTip(tbRemark, "收藏备注信息");
            }
            tbRemark.Text = string.IsNullOrWhiteSpace(remark) ? "<无备注>" : remark;

            tbContent.Anchor = AnchorStyles.None;
            if (status)
            {
                btnStar.Image = Resources.star_active;
                btnStar.Show();
                tbRemark.Show();

                tbContent.Top = 50 + 15;
            }
            else
            {
                btnStar.Image = Resources.star;
                tbRemark.Hide();

                tbContent.Top = 32;
            }
            btnStar.Update();
            Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top | AnchorStyles.Bottom;
            Update();
        }

        private void btnStar_Click(object sender, EventArgs e)
        {
            if (starred)
            {
                if (DialogResult.OK == MessageBox.Show("确定要取消收藏吗？", "风险确认", MessageBoxButtons.OKCancel))
                {
                    ClipboardManager.UnStar(FileName);
                    remark = null;
                    UpdateStarStatus(false);
                }
                return;
            }

            if (starDialog == null)
            {
                starDialog = new StarDialog();
            }

            starDialog.Data = tbContent.Text;
            starDialog.Remark = remark;
            if (starDialog.ShowDialog() == DialogResult.Cancel)
            {
                return;
            }
            remark = starDialog.Remark;
            UpdateStarStatus(true);
            ClipboardManager.Star(FileName, remark);
        }

        private void btnCopy_Click(object sender, EventArgs e)
        {
            ClipboardManager.IgnoreColor = true;
            Clipboard.SetText(tbContent.Text);
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            OnRemove?.Invoke(this, FileName);
        }

        private void tbContent_ContentsResized(object sender, ContentsResizedEventArgs e)
        {
            var offset = 40;
            if (starred)
            {
                offset = 40 + 55;
            }
            var height = e.NewRectangle.Height + offset;
            if (height > offset)
            {
                Height = offset;
            }
            Height = height;
            Update();
        }

        private void ClipboardCtrl_MouseHover(object sender, EventArgs e)
        {
            btnStar.Visible = btnCopy.Visible = btnRemove.Visible = true;
        }

        private void ClipboardCtrl_MouseLeave(object sender, EventArgs e)
        {
            var point = PointToClient(MousePosition);
            // 4 是个偏移量
            var onControl = point.X >= 0 && point.Y >= 0 && point.X < Width - 4 && point.Y < Height - 4;
            btnCopy.Visible = btnRemove.Visible = onControl;
            btnStar.Visible = onControl || starred;

            Application.DoEvents();
        }
    }
}
